<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<!-- HTML file produced from file: manual.tex --
 -- using Hyperlatex v 2.3.1 (c) Otfried Cheong--
 -- on Emacs 21.4 (patch 12) "Portable Code" XEmacs Lucid, Wed Jun  2 18:56:59 2004 -->
<HEAD>
<TITLE>Scheme 48 Manual -- Finite record types</TITLE>

</HEAD><BODY BGCOLOR="#ffffff">
<EM>Scheme 48 Manual</EM> | <A HREF="s48manual.html#top_node">Contents</A> | In Chapter: <A HREF="s48manual_35.html">Libraries</A><BR>Previous: <A HREF="s48manual_46.html">Finite record types</A> | Next: <A HREF="s48manual_46.html">Finite record types</A>
<H2>Finite record types</H2>
<P>The structure <CODE>finite-types</CODE> has
 two macros for defining `finite' record types.
These are record types for which there are a fixed number of instances,
 all of which are created at the same time as the record type itself.
The syntax for defining an enumerated type is:
<BLOCKQUOTE><PRE>
(define-enumerated-type <CODE><I>tag</I></CODE> <CODE><I>type-name</I></CODE>
  <CODE><I>predicate-name</I></CODE>
  <CODE><I>vector-of-instances-name</I></CODE>
  <CODE><I>name-accessor</I></CODE>
  <CODE><I>index-accessor</I></CODE>
  (<CODE><I>instance-name</I></CODE> ...))
</PRE></BLOCKQUOTE>
This defines a new record type, bound to <CODE><I>type-name</I></CODE>, with as many
 instances as there are <CODE><I>instance-name</I></CODE>'s.
<CODE><I>Vector-of-instances-name</I></CODE> is bound to a vector containing the instances
 of the type in the same order as the <CODE><I>instance-name</I></CODE> list.
<CODE><I>Tag</I></CODE> is bound to a macro that when given an <CODE><I>instance-name</I></CODE> expands
 into an expression that returns corresponding instance.
The name lookup is done at macro expansion time.
<CODE><I>Predicate-name</I></CODE> is a predicate for the new type.
<CODE><I>Name-accessor</I></CODE> and <CODE><I>index-accessor</I></CODE> are accessors for the
 name and index (in <CODE><I>vector-of-instances</I></CODE>) of instances of the type.
<P><BLOCKQUOTE><PRE>
(define-enumerated-type color :color
  color?
  colors
  color-name
  color-index
  (black white purple maroon))

(color-name (vector-ref colors 0)) <CODE>-&gt;</CODE> black
(color-name (color white))         <CODE>-&gt;</CODE> white
(color-index (color purple))       <CODE>-&gt;</CODE> 2
</PRE></BLOCKQUOTE>
<P>Finite types are enumerations that allow the user to add additional
 fields in the type.
The syntax for defining a finite type is:
<BLOCKQUOTE><PRE>
(define-finite-type <CODE><I>tag</I></CODE> <CODE><I>type-name</I></CODE>
  (<CODE><I>field-tag</I></CODE> ...)
  <CODE><I>predicate-name</I></CODE>
  <CODE><I>vector-of-instances-name</I></CODE>
  <CODE><I>name-accessor</I></CODE>
  <CODE><I>index-accessor</I></CODE>
  (<CODE><I>field-tag</I></CODE> <CODE><I>accessor-name</I></CODE> [<CODE><I>modifier-name</I></CODE>])
  ...
  ((<CODE><I>instance-name</I></CODE> <CODE><I>field-value</I></CODE> ...)
   ...))
</PRE></BLOCKQUOTE>
The additional fields are specified exactly as with <CODE>define-record-type</CODE>.
The field arguments to the constructor are listed after the <CODE><I>type-name</I></CODE>;
 these do not include the name and index fields.
The form ends with the names and the initial field values for
 the instances of the type.
The instances are constructed by applying the (unnamed) constructor to
 these initial field values.
The name must be first and 
 the remaining values must match the <CODE><I>field-tag</I></CODE>s in the constructor's
 argument list.
<P><BLOCKQUOTE><PRE>
(define-finite-type color :color
  (red green blue)
  color?
  colors
  color-name
  color-index
  (red   color-red)
  (green color-green)
  (blue  color-blue)
  ((black    0   0   0)
   (white  255 255 255)
   (purple 160  32 240)
   (maroon 176  48  96)))

(color-name (color black))         <CODE>-&gt;</CODE> black
(color-name (vector-ref colors 1)) <CODE>-&gt;</CODE> white
(color-index (color purple))       <CODE>-&gt;</CODE> 2
(color-red (color maroon))         <CODE>-&gt;</CODE> 176
</PRE></BLOCKQUOTE>
<P><P>
  
Previous: <A HREF="s48manual_46.html">Finite record types</A> | Next: <A HREF="s48manual_46.html">Finite record types</A></BODY></HTML>
